package com.mju.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mju.dao.VipPriceDao;
import com.mju.pojo.VipPriceInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Objects;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class VipPriceServiceImpl implements IVipPriceService {
    @Autowired
    private VipPriceDao vipPriceDao;

    @Override
    public Boolean addVipPriceInfo(VipPriceInfo vipPrice) {
        log.info("添加会员价格表...");
        int result = vipPriceDao.insert(vipPrice);
        return result > 0;
    }

    @Override
    public Boolean deleteVipPriceInfo(Integer vPid) {
        log.info("删除会员价格表...");
        int result = vipPriceDao.deleteById(vPid);
        return result > 0;
    }

    @Override
    public Boolean updateVipPriceInfo(VipPriceInfo vipPriceInfo) {
        log.info("修改会员价格表...");
        int result = vipPriceDao.updateById(vipPriceInfo);
        return result > 0;
    }

    @Override
    public Boolean updateVipPriceByVPid(Integer vPid, BigDecimal vipPrice) {
        log.info("修改会员价格...");
        VipPriceInfo vipPriceInfo = vipPriceDao.selectById(vPid);
        vipPriceInfo.setVipPrice(vipPrice);
        int result = vipPriceDao.updateById(vipPriceInfo);
        return result > 0;
    }

    @Override
    public BigDecimal getVipPriceByVPid(Integer vPid) {
        log.info("获取会员价格...");
        VipPriceInfo vipPriceInfo = vipPriceDao.selectById(vPid);
        return vipPriceInfo.getVipPrice();
    }

    @Override
    public BigDecimal getDiscountByVipName(String vipName) {
        log.info("获取会员折扣...");
        LambdaQueryWrapper<VipPriceInfo> lwq = new LambdaQueryWrapper<>();
        lwq.eq(!(vipName.isEmpty()), VipPriceInfo::getVipName, vipName);
        VipPriceInfo vipPriceInfo = vipPriceDao.selectOne(lwq);
        return !ObjectUtils.isEmpty(vipPriceInfo) ? vipPriceInfo.getDiscount() : BigDecimal.valueOf(-1.00);
    }

    @Override
    public IPage<VipPriceInfo> queryAllVipPriceInfo(Integer currentPage, Integer pageSize) {
        IPage page = new Page<>(currentPage, pageSize);
        vipPriceDao.selectPage(page, null);
        return page;
    }

    @Override
    public IPage<VipPriceInfo> conQueryAllVipPriceInfo(Integer currentPage, Integer pageSize, VipPriceInfo vipPriceInfo) {
        IPage page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<VipPriceInfo> lwq = new LambdaQueryWrapper<>();
        lwq.eq(!Objects.isNull(vipPriceInfo.getVPid()), VipPriceInfo::getVPid, vipPriceInfo.getVPid());
        lwq.like(!Objects.isNull(vipPriceInfo.getVipName()), VipPriceInfo::getVipName, vipPriceInfo.getVipName());
        vipPriceDao.selectPage(page, lwq);
        return page;
    }
}
